package com.supreme.simpleserver.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.supreme.commonmybatis.mapper.TestMapper;
import com.supreme.commonentity.mysql.primary.MBTest;
import com.supreme.commonmybatis.service.normal.DataScopeService;
import com.supreme.commonboot.utils.AjaxResp;
import com.supreme.simpleserver.service.TestService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping(value = "test")
public class TestController {

    @Resource
    private TestMapper testMapper;

    @Autowired
    private DataScopeService dataScopeService;

    private final TestService testService;

    @Autowired
    public TestController(/*MBTestMapper mbTestMapper, */TestService testService) {
//        this.mbTestMapper = mbTestMapper;
        this.testService = testService;
    }

    @GetMapping(value = "hello")
    public AjaxResp hello() {
        Thread currentThread = Thread.currentThread();
        long threadId = currentThread.getId();
        String threadName = currentThread.getName();
        return new AjaxResp(String.format("threadId: %d threadName: %s", threadId, threadName));
    }

    @GetMapping(value = "search")
    public AjaxResp searchTest(@RequestParam(value = "firstName", required = false) String firstName,
                               @RequestParam(value = "lastName", required = false) String lastName,
                               HttpServletRequest request) {
        String localAddr = request.getLocalAddr();
        int localPort = request.getLocalPort();

        QueryWrapper<MBTest> queryWrapper = new QueryWrapper<>();
        if (firstName != null) {
            queryWrapper.eq("first_name", firstName);
        }
        if (lastName != null) {
            queryWrapper.eq("last_name", lastName);
        }

        Page<MBTest> pageOptions = new Page<>(1, 10);
        Page<MBTest> selectPage = this.testMapper.selectPage(pageOptions, queryWrapper);

        return new AjaxResp(selectPage);
    }

    @GetMapping(value = "dataScopeTestList")
    public AjaxResp dataScopeTestList() {
        final List<MBTest> testList = dataScopeService.testList();
        return new AjaxResp(testList);
    }

    @GetMapping(value = "searchTestList")
    public AjaxResp searchTestList() {
        List<MBTest> testList = this.testService.testList("界界水");
        return new AjaxResp(testList);
    }

    @GetMapping(value = "svcSearch")
    public AjaxResp searchForSvc() {
        List<MBTest> testList = this.testService.svcSearch();
        return new AjaxResp(testList);
    }

    @PostMapping(value = "create")
    public AjaxResp createTest(@RequestBody MBTest test) {
        int insert = this.testMapper.insert(test);
        return new AjaxResp(insert);
    }

    @PostMapping(value = "create2")
    public AjaxResp createTest2(@RequestParam(value = "state", defaultValue = "1") int state, @RequestBody MBTest test) {
        int insert = this.testService.createTest(test, state);
        return new AjaxResp(insert);
    }

    @PostMapping(value = "create3")
    public AjaxResp createTest3(@RequestParam(value = "state", defaultValue = "1") int state, @RequestBody MBTest test) {
        int insert = this.testService.createTest2(test, state);
        return new AjaxResp(insert);
    }
}
